﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class ForumAccessRuleTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [Test]
        public void Can_CRUD()
        {
            #region Prepare

            var role = Processor.Execute(new CreateRole
            {
                Code = "xy"
            }).Result;
            var forum = Processor.Execute(new CreateForum
            {
                ParentID = 0,
                DisplayOrder = 0
            }).Result;

            #endregion

            var rule = Processor.Execute(new CreateForumAccessRule
            {
                ForumID = forum.EntityID,
                RoleID = role.EntityID,
                AccessRights = ForumAccessRights.ReadOnly
            }).Result;

            Assert.NotNull(rule);
            rule.AccessRights.AreEqual(ForumAccessRights.ReadOnly);
            rule.AreEqual(Processor.Execute(new GetForumAccessRule
            {
                EntityID = rule.EntityID
            }).Result);

            var upd_rule = Processor.Execute(new UpdateForumAccessRule
            {
                EntityID = rule.EntityID,
                AccessRights = ForumAccessRights.ReadWrite
            }).Result;

            Assert.NotNull(upd_rule);
            upd_rule.AreEqual(rule);
            upd_rule.AccessRights.AreEqual(ForumAccessRights.ReadWrite);

            Processor.Execute(new DeleteForumAccessRule
            {
                EntityID = rule.EntityID
            });
        }

        [Test]
        public void Can_SetOp()
        {
            #region Prepare

            var role = Processor.Execute(new CreateRole
            {
                Code = "xy"
            }).Result;
            var forum = Processor.Execute(new CreateForum
            {
                ParentID = 0,
                DisplayOrder = 0
            }).Result;

            #endregion

            var rule = Processor.Execute(new SetForumAccessRule
            {
                ForumID = forum.EntityID,
                RoleID = role.EntityID,
                AccessRights = ForumAccessRights.ReadOnly
            }).Result;

            rule.AccessRights.AreEqual(ForumAccessRights.ReadOnly);

            rule = Processor.Execute(new SetForumAccessRule
            {
                ForumID = forum.EntityID,
                RoleID = role.EntityID,
                AccessRights = ForumAccessRights.ReadWrite
            }).Result;

            rule.AccessRights.AreEqual(ForumAccessRights.ReadWrite);

            Processor.Execute(new DeleteForumAccessRule
            {
                EntityID = rule.EntityID
            });
        }
    }
}
